package com.czkt.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.shop.pojo.*;
import com.czkt.shop.pojo.vo.ProductSpuVo;
import com.czkt.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-09-04
 */
@RestController
@RequestMapping("/productSpu")
@CrossOrigin
public class ProductSpuController {

    @Autowired
    IProductSpuService productSpuService;

    @Autowired
    IProductSkuService productSkuService;

    @Autowired
    ISkuImgService skuImgService;

    @Autowired
    ISpeNameService speNameService;

    @Autowired
    ISkuValueService skuValueService;

    @RequestMapping("/list")
    public Object list(@RequestParam(defaultValue = "1")Integer pageIndex,@RequestParam(defaultValue = "4")Integer pageSize,String token){
        IPage<ProductSpuVo> page = new Page<>(pageIndex,pageSize);

        IPage<ProductSpuVo> page1 = productSpuService.getPages(page);

        return page1;
    }

    @RequestMapping("/detail")
    public Object detail(Integer skuId){

        System.out.println(skuId);

        List<SkuImg> imgList = skuImgService.list(skuId);

        ProductSku productSku = productSkuService.getById(skuId);

        ProductSpu productSpu = productSpuService.getById(productSku.getSpuId());

        Map<String,Object> map = new HashMap<>();

        map.put("imgList",imgList);

        map.put("productSku",productSku);

        map.put("productSpu",productSpu);

        return map;
    }

    @RequestMapping("/attr")
    public Object getAttr(Long spuId){
        List<SpeName> speNameList = speNameService.list(spuId);

        System.out.println(speNameList);

        Set<Integer> speNameSet = speNameList.stream().map(item->{
            return item.getId();
        }).collect(Collectors.toSet());

        System.out.println(speNameSet);

        List<SkuValue> skuValues = skuValueService.list(speNameSet);

        Map<String,Object> map = new HashMap<>();

        map.put("speNameList",speNameList);

        map.put("skuValues",skuValues);

        return map;
    }

    @RequestMapping("/attrSku")
    public Object attrSku(String attr,Long spuId){

        ProductSku productSkus = productSkuService.list(attr,spuId);


        return productSkus;
    }


}
